﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Windows.Forms;

namespace C4F.VistaP2P.Win.File
{
    public class FileDesignerActionList : DesignerActionList
    {
        /// <summary>
        /// This class enables WinForms Design Time support
        /// </summary>
        public FileDesignerActionList(IComponent component)
            : base(component)
        {
        }

        /// <summary>
        /// The list of items to show in the smart tag on the design form.
        /// </summary>
        /// <returns></returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {

            // Create list to store designer action items
            DesignerActionItemCollection actionItems = new DesignerActionItemCollection();


            //// Add Design category header text
            actionItems.Add(new DesignerActionHeaderItem("P2P Controls"));

            // Add Design category descriptive label
            actionItems.Add(
              new DesignerActionTextItem(
                "Properties that affect which control the File Control uses.",
                "Design"));

            // Dock/Undock designer action method item
            actionItems.Add(
              new DesignerActionMethodItem(
                this,
                "ToggleDockStyle",
                GetDockStyleText(),
                "Design",
                "Dock or undock this control in it's parent container.",
                true));

            actionItems.Add(
               new DesignerActionPropertyItem(
                 "Visible",
                 "Visible",
                 GetCategory(FileControl, "Visible"),
                 GetDescription(FileControl, "Visible")));  
       
            actionItems.Add(
              new DesignerActionPropertyItem(
                "StatusLog",
                "Status Log",
                GetCategory(this.FileControl, "StatusLog"),
                GetDescription(this.FileControl, "StatusLog")));

            actionItems.Add(
             new DesignerActionPropertyItem(
               "SendFileButton",
               "Send File Button",
               GetCategory(this.FileControl, "SendFileButton"),
               GetDescription(this.FileControl, "SendFileButton")));

            // Return action list to design time item
            return actionItems;
        }


        /// <summary>
        /// The Status log control to hook up if they select it
        /// </summary>
        public TextBox StatusLog
        {
            get { return this.FileControl.StatusLog; }
            set { SetProperty("StatusLog", value); }
        }

        /// <summary>
        /// The Send File control to hook up if they select it.
        /// </summary>
        public Button SendFileButton
        {
            get { return this.FileControl.SendFileButton; }
            set { SetProperty("SendFileButton", value); }
        }

        /// <summary>
        /// Set if this control should be visible or not
        /// </summary>
        public bool Visible
        {
            get { return this.FileControl.ShadowVisible; }
            set { SetProperty("ShadowVisible", value); }
        }


        
        /// <summary>
        /// Set the docking mode 
        /// </summary>
        public void ToggleDockStyle()
        {

            // Toggle ClockControl's Dock property
            if (this.FileControl.Dock != DockStyle.Fill)
            {
                SetProperty("Dock", DockStyle.Fill);
            }
            else
            {
                SetProperty("Dock", DockStyle.None);
            }
        }
 
        /// <summary>
        /// Helper method that returns an appropriate
        /// display name for the Dock/Undock property.
        /// </summary>
        /// <returns></returns>
        private string GetDockStyleText()
        {
            if (this.FileControl.Dock == DockStyle.Fill)
            {
                return "Undock in parent container";
            }
            else
            {
                return "Dock in parent container";
            }
        }

        
        /// <summary>
        /// Helper property to acquire a FileControl reference
        /// </summary>
        private FileControl FileControl
        {
            get { return (FileControl)this.Component; }
        }

        
         
        /// <summary>
        /// Helper method to safely set a component’s property
        /// </summary>
        /// <param name="propertyName">the property name to set</param>
        /// <param name="value">the value to set</param>
        private void SetProperty(string propertyName, object value)
        {
            // Get property
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.FileControl)[propertyName];
            // Set property value
            property.SetValue(this.FileControl, value);
        }

        /// <summary>
        /// Helper method to return the Category string from a
        /// CategoryAttribute assigned to a property exposed by 
        /// the specified object
        /// </summary>
        /// <param name="source">the control to set</param>
        /// <param name="propertyName">the property to set</param>
        /// <returns>the category the property is part of</returns>
        private static string GetCategory(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            CategoryAttribute attribute = (CategoryAttribute)property.GetCustomAttributes(typeof(CategoryAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Category;
        }

        
        /// <summary>
        /// Helper method to return the Description string from a
        /// DescriptionAttribute assigned to a property exposed by 
        /// the specified object
        /// </summary>
        /// <param name="source">the control</param>
        /// <param name="propertyName">the property to get the description from</param>
        /// <returns>the description for this property</returns>
        private static string GetDescription(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            DescriptionAttribute attribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Description;
        }
    }
}
